#ifndef __EEDIT_MEM__
#define __EEDIT_MEM__

#include "eedit_syntax.h"

#define STANDARD_LINE 0x01
#define FOLD_START 0x02
#define FOLDED 0x04
#define FOLD_END 0x08

typedef int CHARACTER;
#define SIZE_OF_CHARACTER sizeof(CHARACTER)

typedef long POSITION;
#define SIZE_OF_POSITION sizeof(POSITION)

#define LINE_SIZE 120

#define DEBUG_CHECK_BOUNDS(a,b) if(a<b){\
    printf("DEBUG_CHECK_BOUNDS failed %i < %i in %s",\
            a,b,__PRETTY_FUNCTION__);\
}

#define DEBUG_FATAL(a) {\
    printf("Fatal Message: %s\n From: %s\n",\
            a,__PRETTY_FUNCTION__);\
    exit(1);\
}

#define END_CHECK(line,buffer) {\
    if(line->next==NULL){\
        assert(line==buffer->tail);\
        return 0;\
    }else{\
        return 1;\
    }\
}


/*
 * A line can be simply a list of characters _or_ a fold.
 * A fold has a seperate internal list of folded lines
 */
typedef struct{
    int type;               
    int length; /* Number of used characters (inc '\n') */
    int display_len; /* equals length _unless_ we are folding */
    struct line_t* prev;
    struct line_t* next;
    struct syntax_token_t* syn;    /* Syntax.h */
    CHARACTER* mem;
}line_t;

typedef struct{
    unsigned long length;   /* In lines */
    struct line_t* head;
    struct line_t* tail;
    int cursorLine;
    int cursorCol;
    /* These allow changes to be made in context */
    int prev_cursorLine;
    int prev_position;
}file_buffer_t;

/* Create a new empty buffer */
file_buffer_t* newBuffer();

/* Delete (free) a buffer */
void delBuffer(file_buffer_t*);

/* Add a new line to a buffer. 
 * if int > 0 then the line will be inserted
 * at the end of line /int/ */
void addNewLine(file_buffer_t*);

/* Remove int line from the buffer. */
void delBufferLine(file_buffer_t*,int);

/* Return one line of the buffer */
line_t* getBufferLine(file_buffer_t*,int);

/* Add a character to the buffer */
void addBufferChar(file_buffer_t*, CHARACTER, POSITION);

/* Remove a character */
void delChar(file_buffer_t*, POSITION);

/* Add a string to the buffer, starting from POSITION*/
void addBufferStr(file_buffer_t*, CHARACTER*, POSITION);

/* Create a fold around this line */
void createFold(POSITION);
void unfold(POSITION);

#endif
